package com.ptmall.app.bean.model.db;

import com.ptmall.app.App;
import com.ptmall.app.bean.model.db.dao.DaoMaster;
import com.ptmall.app.bean.model.db.dao.DaoSession;

import org.greenrobot.greendao.database.Database;


/**
 * DBHelper   2016-11-23
 * <p>
 * Copyright(c) 2016 xianshangyun co.ltd. All right reserved
 */

public class DBHelper {
    private static final String DB_NAME = "ptmall.db";

    private static volatile DaoSession sInstance;

    private static Database mDatabase;

    private DBHelper() {
        // Avoid direct instantiate
    }

    public synchronized static DaoSession getInstance() {
        if (sInstance == null) {
            synchronized (DBHelper.class) {
                if (sInstance == null) {
                    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(App.getInstance(), DB_NAME);

                    mDatabase = helper.getWritableDb();
                    //Database 这个地方可以开启事物
                    sInstance = new DaoMaster(mDatabase).newSession();
                    return sInstance;
                }
            }
        }
        return sInstance;
    }

    public static Database getDatabase() {
        return mDatabase;
    }


}
